<template>
  <div class="my-container">
    <!-- 已登录头部 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <!-- <van-image
            class="avatar"
            src="https://img.yzcdn.cn/vant/cat.jpeg"
            fit="cover"
          /> -->
          <div class="avatar cat">
          </div>
          <span class="name">username</span>
        </div>
        <!-- <div class="right">
          <van-button size="mini" round @click="$router.push('/redact')">编辑资料</van-button>
        </div> -->
      </div>
    </div>
    <div class="header user-info" v-if="Puser">
      <div class="base-info">
        <div class="left">
          <van-image
            class="avatar"
            :src="'http://upenglish.ayvvv.cn/' + parentInfo.headimage"
            round
            fit="cover"
          />
          <span class="name">{{ parentInfo.parentname }}</span>
        </div>
        <!-- <div class="right">
          <van-button size="mini" round @click="$router.push('/redact')">编辑资料</van-button>
        </div> -->
      </div>
    </div>
    <!-- /已登录头部 -->

    <!-- 未登录头部 -->
    <div v-if="user === false && Puser === false" class="header not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- /未登录头部 -->

    <!-- 正在上课 -->
    <div id="one"  class="classing" v-if="user">
      <div class="classing_h3" v-if="haveteacher">
        <span><span style="color:red">主讲老师</span> ：<span @click="jump">{{teachername}}</span></span>
        <div class="looktime">开始时间：<span>{{stime | dateFormat}}</span></div>
      </div>
      <div class="noteacher" v-if="nohaveteacher">
        <span style="color:blue;">当前暂无老师直播</span>
      </div>
      <!-- <div class="classing_b">
        <span class="classing_h">时间 : 1:00</span>
        <span class="classing_h">观看人数 : 4</span>
      </div> -->
    </div>
    <!-- 正在上课 -->
    <van-cell v-if="user" title="学习记录" is-link @click="$router.push('/history')" />
    <van-cell v-if="user" title="考试记录" is-link @click="$router.push('/myquestion')" />
    <van-cell v-if="user" title="我的题库" is-link @click="$router.push('/message')" />
    <van-cell v-if="user" title="购买记录" is-link @click="$router.push('/record')" />
    <van-cell v-if="user" class="mb-9" title="客服中心" is-link @click="kefu" />
    <van-cell
      class="logout-cell"
      clickable
      title="退出登录"
      v-if="user"
      @click="outLogout"
    />
    <van-cell
      class="logout-cell"
      clickable
      title="家长登录"
      v-if="user"
      @click="onLogout"
    />
    <van-cell
      style="color:red"
      clickable
      :title="(parentInfo.answerswitch === 0 ? '开启' : '关闭') + '考试答案'"
      @click="getclick"
      v-if="Puser"
    />
      <van-cell
      class="logout-cell"
      clickable
      title="学生登录"
      v-if="Puser"
      @click="h"
    />
    <div id="bottomdiv">
    </div>
  </div>
</template>

<script>
import { info, kaiguan, newteacher } from '@/api/user'
import { mapState } from 'vuex'
export default {
  name: 'MyIndex',
  components: {},
  props: {},
  data () {
    return {
      userInfo: {},
      parentInfo: [],
      switch: [],
      teachername: '',
      appurl: '',
      stime: '',
      etime: '',
      haveteacher: false,
      nohaveteacher: false
    }
  },
  computed: {
    ...mapState(['user', 'Puser', 'Pid', 'id'])
  },
  watch: {},
  created () {
    if (this.user) {
      this.teacher()
    } else if (this.Puser) {
      this.getInfo()
    } else {
      this.$router.push('/login')
    }
  },
  mounted () {},
  methods: {
    jump () {
      window.location.href = this.appurl
    },
    onLogout () {
      this.$dialog.confirm({
        title: '确认进入家长登录？'
      }).then(() => {
        this.$store.commit('setUser', null)
        this.$store.commit('setPuser', null)
        this.$router.push('/deng')
      }).catch(() => {
        console.log('取消执行这里')
      })
    },
    outLogout () {
      this.$dialog.confirm({
        title: '确认退出当前登录？'
      }).then(() => {
        this.$store.commit('setUser', null)
        this.$store.commit('setPuser', null)
        this.$router.push('/')
      }).catch(() => {
        console.log('取消执行这里')
      })
    },
    async teacher () {
      const { data } = await newteacher({
        userid: this.id
      })
      if (data.code === 203) {
        this.nohaveteacher = true
      } else {
        this.haveteacher = true
      }
      console.log(data, '00')
      this.teachername = data.result.teachername
      this.appurl = data.result.appurl
      console.log(data.result, '55')
      this.stime = data.result.startime
      this.etime = data.result.endtime
    },
    h () {
      this.$dialog.confirm({
        title: '确认进入学生登录吗？'
      }).then(() => {
        this.$store.commit('setUser', null)
        this.$store.commit('setPuser', null)
        this.$router.push('/login')
      }).catch(() => {
        console.log('取消执行这里')
      })
    },
    // async loadUserInfo () {
    //   try {
    //     const data = await getUserInfo()
    //     this.userInfo = data.data
    //     console.log(this.userInfo)
    //   } catch (err) { // this.$toast('获取数据失败，请稍后重试')
    //   }
    // },
    async getInfo () {
      try {
        const { data } = await info()
        console.log(data)
        this.parentInfo = data.parentresult
        this.switch = data.parentresult.answerswitch
        // console.log(this.switch)
      } catch (err) {
        console.log(err)
      }
    },
    async getclick () {
      // console.log(this.switch)
      if (this.switch === 1) {
        this.switch = 0
      } else {
        this.switch = 1
      }
      const { data } = await kaiguan({
        parentid: this.Pid,
        answerswitch: this.switch
      })
      console.log(data)
      this.$toast.success('修改成功')
      this.getInfo()
    },
    kefu () {
      window.location.href = 'http://webim.ayvvv.cn/index/index/user?kefu_code=kefu5fa2220d78cc4&keyword=up%E8%8B%B1%E8%AF%AD&namennnn=%E6%AD%A6%E5%A5%B3%E5%A3%AB'
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .header {
    height: 240px;
    background: url("~@/assets/banner.png");
    background-size: cover;
  }

  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 38px;
        color: #fff;
      }
    }
  }

  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
          margin-right: 23px;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .text {
          font-size: 23px;
        }
      }
    }
  }

  .grid-nav {
    .grid-item {
      height: 141px;
      i.iconfont {
        font-size: 45px;
      }
      .icon-shoucang {
        color: #eb5253;
      }
      .icon-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }

  .logout-cell {
    text-align: center;
    color: #d86262;
  }

  .mb-9 {
    margin-bottom: 9px;
  }

  .classing {
    width: 100%;
    height: 196px;
    box-sizing: border-box;
    .classing_h3 {
      text-align: center;
      padding-top: 40px;
      padding-bottom: 40px;
    }
    .classing_b {
       display: flex;
       justify-content: space-around;
    }
  }
}
#one{
  box-shadow:0px 0px 15px dimgray;
  margin-bottom:30px;
  margin-top: 10px;
}
html{
  body{
      #app{
    padding-bottom:0px  !important
  }
  }
}
#bottomdiv{
   background: url(../../img/stu.jpg);
   background-size:100%;
   background-repeat: no-repeat;
   height: 300px;
}
.cat {
  background: url('~@/assets/cat.png');
  background-size: 100%;
}
.classing_b  .classing_h{
  font-size: 26px  !important;
}
.looktime{
  font-size: 14px;
  margin-top: 10px;
}
.noteacher {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
